package com.myway.service.ibe.command;

import com.myway.dto.domain.Cabin;
import com.myway.dto.domain.FlightCriteria;
import com.myway.dto.domain.Segment;
import com.myway.dto.param.CabinsQuantityParam;
import com.myway.service.ibe.AVCommandService;
import com.travelsky.ibe.client.*;
import com.travelsky.util.QDateTime;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by yufei on 2018/8/13.
 */
public class AVCommand extends BaseIBECommand implements AVCommandService {

    public List<Cabin> getCabinNum(CabinsQuantityParam criteria) {
        List<Cabin> cabins = null;
        List<Cabin> allcabins = new ArrayList<Cabin>();
        try {
            AV av = new AV();
            Date orgDate = QDateTime.stringToDate(criteria.getFlightDate(), "yyyy-MM-dd");
            //航班起飞地三字码
            String org = criteria.getOrg();
            //航班到达地三字码
            String dst = criteria.getDst();
            AVReq avReq = new AVReq();
            avReq.setOrg(org);
            avReq.setDst(dst);
            avReq.setDateOfFlight(orgDate);
            avReq.setAirline(criteria.getAirline());
            AvResult result = av.getAvailability(avReq);
            //获取要查询的舱位
            cabins = criteria.getCabins();
            if (result != null) {
                for (int i = 0; i < result.getItemamount(); i++) {
                    AvItem ai = result.getItem(i);
                    for (int j = 0; j < ai.getSegmentnumber(); j++) {
                        AvSegment as = ai.getSegment(j);
                        for (int k = 0; k < 26; k++) {
                            char cangwei = as.getCangweiCodeSort(k);
                            if (cangwei == '-') {
                                break;
                            }
                            Cabin cabin = new Cabin();
                            char cabinInfo = as.getCangweiinfoOfSort(as.getCangweiCodeSort(k));
                            cabin.setCabinName(String.valueOf(cangwei));
                            cabin.setQuantity(String.valueOf(cabinInfo));
                            cabin.setFlydistance(as.getFlydistance());
                            allcabins.add(cabin);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("---av---" + e.getMessage());
        }
        if (allcabins.size() > 0) {
            for (Cabin a : allcabins) {
                for (int m = 0; m < cabins.size(); m++) {
                    Cabin cabin = cabins.get(m);
                    String cabinName = cabin.getCabinName();
                    if (a.getCabinName().equals(cabinName)) {
                        cabin.setQuantity(a.getQuantity());
                    }
                }
            }
        }
        return cabins;
    }

    /**
     * 根据航班号，日期查询飞行距离
     *
     * @param airline
     * @param date
     * @return
     */
    public String getFlyDistance(String airline, Date date) {
        int flyDiseance = 0;
        AV av = new AV();
        this.configIBEClient(av);
        AvResult result;
        try {
            result = av.getAvailability(airline, date);
        } catch (Exception e) {
            return null;
        }
        //航班号,日期查询方式 只有一个AVItem
        AvItem item = result.getItem(0);
        for (int i = 0; i < item.getSegmentnumber(); i++) {
            flyDiseance += item.getSegment(i).getFlydistance();
        }
        return String.valueOf(flyDiseance);
    }


    /**
     * 根据航班号 查询
     *
     * @param airline
     * @param date
     * @return
     */
    public Boolean getMealList(String airline, Date date) {
        AV av = new AV();
        this.configIBEClient(av);
        AvResult result;
        try {
            result = av.getAvailability(airline, date);
        } catch (Exception e) {
            return null;
        }
        //航班号,日期查询方式 只有一个AVItem
        AvItem item = result.getItem(0);
        Boolean meal = false;
        for (int i = 0; i < item.getSegmentnumber(); i++) {
            meal = item.getSegment(i).getMeal();
        }
        return meal;
    }
}
